﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using DatabaseManagement.DataObjects;


namespace DatabaseManagement
{
    public class DBManager
    {
        public static DBCollectionManager ColDatabaseManager;
        public static XmlSerializer SerializeDatabase;

        public static DBCollectionManager LoadDatabases(string directory)
        {
            var stream = new StreamReader(string.Format("{0}\\Databases.xml", directory));
            var serializer = new XmlSerializer(typeof(DBCollectionManager));
            ColDatabaseManager = (DBCollectionManager)serializer.Deserialize(stream);
            stream.Close();
            return ColDatabaseManager;
        }

        public static void InitializeInstanceAndDatabase()
        {
            ColDatabaseManager = new DBCollectionManager();
            ColDatabaseManager.ColInstances = new List<Instance>();

            Instance instance = new Instance();
            instance.Name = "Instancia Genérica";
            instance.IsGeneric = true;

            Database database = new Database();
            database.Name = "Base de datos";

            instance.ColDatabases = new List<Database>();
            instance.ColDatabases.Add(database);
            ColDatabaseManager.ColInstances.Add(instance);
        }

        /// <summary>
        /// Hardcode del proyecto actual.
        /// </summary>
        public static void HardCode()
        {
            var instance = new Instance();
            instance.Name = "Instancia1";

            ColDatabaseManager = new DBCollectionManager();
            var db = new Database {Name = "base1"};
            instance.ColDatabases.Add(db);
            db.Name = "db1Name";

            var sp1 = new StoredProcedure {Name = "sp1"};
            var sp2 = new StoredProcedure {Name = "sp2"};

            var column = new Column() {IsNullable = false, Length = 4, Name="id", Type = DatabaseManagement.DataObjects.Enums.VariableType.Int};
            var column2 = new Column() { IsNullable = true, Length = 50, Name = "name", Type = DatabaseManagement.DataObjects.Enums.VariableType.Varchar };

            var table1 = new Table {Name = "prueba tabla", IsMaster = true};
            table1.ColColumns.Add(column);
            table1.ColColumns.Add(column2);

            var table2 = new Table {Name = "prueba tabla2"};

            sp1.Name = "sp1";
            sp1.Text =
                @"USE [Security]
                        GO
                        /****** Object:  StoredProcedure [dbo].[proc_AddressDelete]    Script Date: 02/17/2013 02:03:32 ******/
                        SET ANSI_NULLS ON
                        GO
                        SET QUOTED_IDENTIFIER ON
                        GO

                        ALTER PROCEDURE [dbo].[proc_AddressDelete]
                        (
	                        @IdAddress int
                        )
                        AS
                        BEGIN

	                        SET NOCOUNT OFF
	                        DECLARE @Err int

	                        DELETE
	                        FROM [Address]
	                        WHERE
		                        [IdAddress] = @IdAddress
	                        SET @Err = @@Error

	                        RETURN @Err
                        END";

            sp2.Name = "segundo store";
            sp2.Text = "texto cortooooooo";
            
            db.ColProcedures.Add(sp1);
            db.ColProcedures.Add(sp2);
            db.ColTables.Add(table1);
            db.ColTables.Add(table2);

            ColDatabaseManager.ColInstances = new List<Instance>();

            ColDatabaseManager.ColInstances.Add(instance);
        }

        public static void SaveDatabases(string directory)
        {
                var strea = new StreamWriter(string.Format("{0}\\Databases.xml", directory));
                SerializeDatabase = new XmlSerializer(typeof(DBCollectionManager));
                SerializeDatabase.Serialize(strea, ColDatabaseManager);
                strea.Close();
        }
    }
}
